// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoZombie;
import zombie.core.network.ByteBufferWriter;
import zombie.core.raknet.UdpConnection;
import zombie.core.skinnedmodel.visual.HumanVisual;
import zombie.core.skinnedmodel.visual.IHumanVisual;
import zombie.core.skinnedmodel.visual.ItemVisual;
import zombie.core.skinnedmodel.visual.ItemVisuals;
import zombie.debug.DebugLog;
import zombie.network.GameClient;
import zombie.network.GameServer;
import zombie.network.PacketTypes;
import zombie.util.Type;

public final class SharedDescriptors {
    private static final int DESCRIPTOR_COUNT = 500;
    private static final int DESCRIPTOR_ID_START = 500;
    private static final byte[] DESCRIPTOR_MAGIC = new byte[]{68, 69, 83, 67};
    private static final int VERSION_1 = 1;
    private static final int VERSION_2 = 2;
    private static final int VERSION = 2;
    private static zombie.SharedDescriptors.Descriptor[] PlayerZombieDescriptors = new zombie.SharedDescriptors.Descriptor[10];
    private static final int FIRST_PLAYER_ZOMBIE_DESCRIPTOR_ID = 1000;

    public static void initSharedDescriptors() {
        if (GameServer.bServer) {
            ;
        }
    }

    private static void noise(String string) {
        DebugLog.log("shared-descriptor: " + string);
    }

    public static void createPlayerZombieDescriptor(IsoZombie zombie) {
        if (GameServer.bServer) {
            if (zombie.isReanimatedPlayer()) {
                if (zombie.getDescriptor().getID() == 0) {
                    int int0 = -1;

                    for (int int1 = 0; int1 < PlayerZombieDescriptors.length; int1++) {
                        if (PlayerZombieDescriptors[int1] == null) {
                            int0 = int1;
                            break;
                        }
                    }

                    if (int0 == -1) {
                        zombie.SharedDescriptors.Descriptor[] descriptor0 = new zombie.SharedDescriptors.Descriptor[PlayerZombieDescriptors.length + 10];
                        System.arraycopy(PlayerZombieDescriptors, 0, descriptor0, 0, PlayerZombieDescriptors.length);
                        int0 = PlayerZombieDescriptors.length;
                        PlayerZombieDescriptors = descriptor0;
                        noise("resized PlayerZombieDescriptors array size=" + PlayerZombieDescriptors.length);
                    }

                    zombie.getDescriptor().setID(1000 + int0);
                    int int2 = zombie.PersistentOutfits.instance.pickOutfit("ReanimatedPlayer", zombie.isFemale());
                    int2 = int2 & -65536 | int0 + 1;
                    zombie.setPersistentOutfitID(int2);
                    zombie.SharedDescriptors.Descriptor descriptor1 = new zombie.SharedDescriptors.Descriptor();
                    descriptor1.bFemale = zombie.isFemale();
                    descriptor1.bZombie = false;
                    descriptor1.ID = 1000 + int0;
                    descriptor1.persistentOutfitID = int2;
                    descriptor1.getHumanVisual().copyFrom(zombie.getHumanVisual());
                    ItemVisuals itemVisuals = new ItemVisuals();
                    zombie.getItemVisuals(itemVisuals);

                    for (int int3 = 0; int3 < itemVisuals.size(); int3++) {
                        ItemVisual itemVisual = new ItemVisual((ItemVisual)itemVisuals.get(int3));
                        descriptor1.itemVisuals.add(itemVisual);
                    }

                    PlayerZombieDescriptors[int0] = descriptor1;
                    noise("added id=" + descriptor1.getID());

                    for (int int4 = 0; int4 < GameServer.udpEngine.connections.size(); int4++) {
                        UdpConnection udpConnection = (UdpConnection)GameServer.udpEngine.connections.get(int4);
                        ByteBufferWriter byteBufferWriter = udpConnection.startPacket();

                        try {
                            PacketTypes.PacketType.ZombieDescriptors.doPacket(byteBufferWriter);
                            descriptor1.save(byteBufferWriter.bb);
                            PacketTypes.PacketType.ZombieDescriptors.send(udpConnection);
                        } catch (Exception exception) {
                            exception.printStackTrace();
                            udpConnection.cancelPacket();
                        }
                    }
                }
            }
        }
    }

    public static void releasePlayerZombieDescriptor(IsoZombie zombie) {
        if (GameServer.bServer) {
            if (zombie.isReanimatedPlayer()) {
                int _int = zombie.getDescriptor().getID() - 1000;
                if (_int >= 0 && _int < PlayerZombieDescriptors.length) {
                    noise("released id=" + zombie.getDescriptor().getID());
                    zombie.getDescriptor().setID(0);
                    PlayerZombieDescriptors[_int] = null;
                }
            }
        }
    }

    public static zombie.SharedDescriptors.Descriptor[] getPlayerZombieDescriptors() {
        return PlayerZombieDescriptors;
    }

    public static void registerPlayerZombieDescriptor(zombie.SharedDescriptors.Descriptor desc) {
        if (GameClient.bClient) {
            int int0 = desc.getID() - 1000;
            if (int0 >= 0 && int0 < 32767) {
                if (PlayerZombieDescriptors.length <= int0) {
                    int int1 = (int0 + 10) / 10 * 10;
                    zombie.SharedDescriptors.Descriptor[] descriptor = new zombie.SharedDescriptors.Descriptor[int1];
                    System.arraycopy(PlayerZombieDescriptors, 0, descriptor, 0, PlayerZombieDescriptors.length);
                    PlayerZombieDescriptors = descriptor;
                    noise("resized PlayerZombieDescriptors array size=" + PlayerZombieDescriptors.length);
                }

                PlayerZombieDescriptors[int0] = desc;
                noise("registered id=" + desc.getID());
            }
        }
    }

    public static void ApplyReanimatedPlayerOutfit(int outfitID, String outfitName, IsoGameCharacter chr) {
        IsoZombie isoZombie = Type.tryCastTo(chr, IsoZombie.class);
        if (isoZombie != null) {
            short _short = (short)(outfitID & 65535);
            if (_short >= 1 && _short <= PlayerZombieDescriptors.length) {
                zombie.SharedDescriptors.Descriptor descriptor = PlayerZombieDescriptors[_short - 1];
                if (descriptor != null) {
                    isoZombie.useDescriptor(descriptor);
                }
            }
        }
    }

    public static final class Descriptor implements IHumanVisual {
        public int ID = 0;
        public int persistentOutfitID = 0;
        public String outfitName;
        public final HumanVisual humanVisual = new HumanVisual(this);
        public final ItemVisuals itemVisuals = new ItemVisuals();
        public boolean bFemale = false;
        public boolean bZombie = false;

        public int getID() {
            return this.ID;
        }

        public int getPersistentOutfitID() {
            return this.persistentOutfitID;
        }

        @Override
        public HumanVisual getHumanVisual() {
            return this.humanVisual;
        }

        @Override
        public void getItemVisuals(ItemVisuals _itemVisuals) {
            _itemVisuals.clear();
            _itemVisuals.addAll(this.itemVisuals);
        }

        @Override
        public boolean isFemale() {
            return this.bFemale;
        }

        @Override
        public boolean isZombie() {
            return this.bZombie;
        }

        @Override
        public boolean isSkeleton() {
            return false;
        }

        public void save(ByteBuffer output) throws IOException {
            byte _byte = 0;
            if (this.bFemale) {
                _byte = (byte)(_byte | 1);
            }

            if (this.bZombie) {
                _byte = (byte)(_byte | 2);
            }

            output.put(_byte);
            output.putInt(this.ID);
            output.putInt(this.persistentOutfitID);
            zombie.GameWindow.WriteStringUTF(output, this.outfitName);
            this.humanVisual.save(output);
            this.itemVisuals.save(output);
        }

        public void load(ByteBuffer input, int WorldVersion) throws IOException {
            this.humanVisual.clear();
            this.itemVisuals.clear();
            byte _byte = input.get();
            this.bFemale = (_byte & 1) != 0;
            this.bZombie = (_byte & 2) != 0;
            this.ID = input.getInt();
            this.persistentOutfitID = input.getInt();
            this.outfitName = zombie.GameWindow.ReadStringUTF(input);
            this.humanVisual.load(input, WorldVersion);
            short _short = input.getShort();

            for (int _int = 0; _int < _short; _int++) {
                ItemVisual itemVisual = new ItemVisual();
                itemVisual.load(input, WorldVersion);
                this.itemVisuals.add(itemVisual);
            }
        }
    }

    private static final class DescriptorList extends ArrayList<zombie.SharedDescriptors.Descriptor> {
    }
}
